$(function() {
    var curPage = 1;
    var pageSize = 24;
    var brand = {
        initPaging: function(totalPage, totalDataNum, res) {
            var self = this,
                ele = $('[data-role="dirPaging"]');
                if(totalDataNum > 0) {
                    ele.show();
                    var pagination = ele.pagination(totalDataNum, {
                        num_edge_entries: 1,
                        num_display_entries: 4,
                        callback: function(page_index) {
                            if(page_index == 0) {
                                var totalPage = res.data.pager.pages;

                                var pageData = res.data.pager.list;
                                var pager = res.data.pager;
                                brand.randerList(pageData);
                            } else {
                                curPage = page_index + 1;
                                brand.getPageDates();
                            }
                        },
                        items_per_page: pageSize,
                        prev_text: "&lt;&lt;上一页",
                        next_text: "下一页&gt;&gt;",
                        link_to: "javascript:;"
                    });

                }else {
                    ele.hide();
                    $('[data-role="productsListCol"]').html('<p style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</p>')
                    productContainer.html('<p style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</p>');
                }

        },
        randerList: function(pageData) {
            var tpl ='';
            $.each(pageData, function(index, item) {
                tpl += '<li class="brand-item">\
                            <dl>\
                                <dt>\
                                    <a href="./brandList.html?brandId='+item.brandId+'">\
                                        <img src="'+item.brandLogoUrl+'" alt="'+item.brandName+'">\
                                    </a>\
                                </dt>\
                                <dd>\
                                    <a href="./brandList.html?brandId='+item.brandId+'">'+item.brandName+'</a>\
                                </dd>\
                            </dl>\
                        </li>';
            })
            $('.brand-list').html(tpl);

        },
        getPageDates: function() {
            var self = this;
            var totalData = {};
            totalData.pageNum = curPage;
            totalData.pageSize = pageSize;
            $.ajax({
                url: wsCommon.host + '/api/brand/list',
                dataType: wsCommon.jsonType,
                data: totalData,
                success: function(res) {

                    if(res.code == 1) {
                        var pageData = res.data.pager.list;
                        var pager = res.data.pager;
                        self.randerList(pageData, pager);
                    }
                },
                error: function() {
                    // console.log('分页接口失败');
                }
            });
        },
        init: function() {
            $.ajax({
                url: wsCommon.host + '/api/brand/list',
                dataType: wsCommon.jsonType,
                data: {
                    pageSize: pageSize
                },
                success: function(res) {
                    if(res.code == 1) {
                        var pager = res.data.pager;
                        // 初始化分页
                        curPage = 1;
                        brand.initPaging(pager.pages, pager.total, res)
                    }
                },
                error: function() {}
            })
        }
    }

    brand.init();
})
